//
//  HomeTableViewCell.swift
//  weiboLQ
//
//  Created by 半缘君 on 15/12/11.
//  Copyright © 2015年 banyuanjun.cn. All rights reserved.
//

import UIKit

class HomeTableViewCell:UITableViewCell {
    //MARK: - 懒加载控件
    lazy var sepView :UIView = {
       let v = UIView()
        v.backgroundColor = UIColor.darkGrayColor()
        return v
    }()
    /// 顶部视图
    lazy var topView = HomeTopView(frame: CGRectZero)
    /// 正文
    var centerLabel = UILabel(txt: "正文", txtFont: 12, alignment: NSTextAlignment.Left)
    /// 配图
    lazy var statusPicture = StatusPictureView()
    /// 底部view
    lazy var bottomView = HomeBottomView(frame: CGRectZero)
    
    //模型属性
    var viewModel:StatusViewModel?{
        didSet {
            topView.viewModel = viewModel
            centerLabel.text = viewModel?.status.text
            
            statusPicture.viewModel = viewModel
//
            statusPicture.snp_updateConstraints { (make) -> Void in
//                make.size.equalTo(statusPicture.bounds.size)
                make.width.equalTo(statusPicture.bounds.width)
                make.height.equalTo(statusPicture.bounds.height)
//                print("Picture \(statusPicture.bounds.width)")
            }
        }
    }
    
    //MARK: - 构造方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
}
// MARK: - 添加控件并设置约束
extension HomeTableViewCell {
    /// 添加控件
    func setupUI () {
        addSepView()
        addTopView()
        addCenterLable()
//        addStatusPictureView()
        contentView.addSubview(statusPicture)
        addBottomView()
        
    }
    ///添加分割视图并设置约束
    private func addSepView () {
        contentView.addSubview(sepView)
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView)
            make.right.equalTo(contentView)
            make.height.equalTo(LQHomeCellMargin)
        }
    }
    /// 添加topView并设置约束
    private func addTopView () {
        contentView.addSubview(topView)
        
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom).offset(LQHomeCellMargin)
            make.left.equalTo(sepView)
            make.right.equalTo(sepView)
            make.height.equalTo(65)
        }
    }
    /// 添加中间lable并设置约束
    private func addCenterLable () {
        contentView.addSubview(centerLabel)
        centerLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(LQHomeCellMargin)
            make.left.equalTo(contentView.snp_left).offset(LQHomeCellMargin)
//            make.right.equalTo(contentView.snp_right).offset(-1 * LQHomeCellMargin)
//            make.bottom.equalTo(contentView).offset(-1 * LQHomeCellMargin)
//            make.width.equalTo(contentView).offset(-2 * LQHomeCellMargin)
        }
        
//        centerLabel.preferredMaxLayoutWidth
    }
/// 添加pictureView
//    private func addStatusPictureView () {
//        contentView.addSubview(statusPicture)
//        statusPicture.snp_makeConstraints { (make) -> Void in
//            make.top.equalTo(centerLabel.snp_bottom)
//            make.left.equalTo(contentView).offset(LQHomeCellMargin)
//            make.width.equalTo(300)
//            make.height.equalTo(90)
//        }
//    }
/// 添加底部view
    private func addBottomView () {

        contentView.addSubview(bottomView)
        bottomView.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(statusPicture.snp_bottom).offset(LQHomeCellMargin)
            make.left.equalTo(contentView)
            make.right.equalTo(contentView)
            make.height.equalTo(44)
//            make.bottom.equalTo(contentView)
        }
    }
    
    
    func getRowHeightWithStatue (status:StatusViewModel) -> CGFloat {
        
        viewModel = status
        
        contentView.layoutIfNeeded()
     return CGRectGetMaxY(bottomView.frame)
    }
}
